package yto.freemarker.springaop.servlet;


import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * web的就是Springmvc的dispatcher的过程
 *
 * @author dongxing
 **/
public class MyInitializar implements WebApplicationInitializer {

    /**
     *
     * @param servletContext
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        /**
         * 相当于web.xml文件中的
         *<servlet>
         *    <servlet-class>XXXX</servlet-class>
         *</servlet>
         *
         */
//        servletContext.addServlet();

        /**
         * 初始化环境
         *
         * 配置一个ContextLoaderListener---执行spring的初始化
         */
//        servletContext.addListener();


        //init web

        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(AppWebConfig.class);
        //这个初始化动作（这个动作很重要）
        annotationConfigApplicationContext.refresh();

        DispatcherServlet dispatcherServlet = new DispatcherServlet((WebApplicationContext) annotationConfigApplicationContext);
        //注册
        ServletRegistration.Dynamic registration = servletContext.addServlet("web", dispatcherServlet);
        registration.addMapping("*.do");
        registration.setLoadOnStartup(1);


    }
}
